<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\InsureCompany;
use App\Models\InsureLogs;
use Illuminate\Http\Request;
use App\Models\Car\CarCost;
// 商业保险单
class InsureController extends Controller
{
    use ResponseData;
    public function index(InsureLogs $model,Request $request) {
        $size = $request->input('size',10);
        $model = $model->with('car');
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('company_name')){
            $model = $model->where('company_name','like','%'.$request->company_name.'%');
        }
        if($request->filled('search')){
            $search = $request->search;
            $model = $model->where('company_name','like','%'.$search.'%')->orWhereHas('car', function($query)use($search) {
                $query->where('gear_car_name', 'like', '%'.$search.'%')->orWhere('plate_number', 'like', '%'.$search.'%');
            });
        }
        $data = $model->paginate($size);
        return $this->success($data);
    }
    public function store(InsureLogs $model,Request $request){
        $data = $request->all();
        $model->fill($data);
        $model->save();
        if($request->filled('company_name')){
            $this->isCompany($request->company_name);
        }
        $this->CostPrice($request->car_id,$request->insurance_premium);
        return $this->success('保存成功');

    }
    protected function CostPrice($car_id,$price){
        $isCarCost = CarCost::query()->where('car_id',$car_id)->first();
        if($isCarCost){
            $isCarCost->increment('total_insurance_premium',$price);
        }else{
            CarCost::query()->create(['car_id'=>$car_id,'total_insurance_premium'=>$price]);
        }
    }
    // 设置公司
    protected function isCompany($name){
        $company = InsureCompany::query()->where('name',$name)->first();
        if(!$company){
            InsureCompany::query()->create(['name'=>$name]);
        }
    }
    public function edit(InsureLogs $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function update(InsureLogs $model,Request $request){
        $data = $model->find($request->id);
        $navPirce = $data->insurance_premium;
        $data->update($request->all());


        if($navPirce != $request->insurance_premium){
            CarCost::query()->where('car_id',$request->car_id)->decrement('total_insurance_premium',$navPirce);
            CarCost::query()->where('car_id',$request->car_id)->increment('total_insurance_premium',$request->insurance_premium);
        }
        return $this->success('修改成功');
    }
    public function destory(InsureLogs $model,Request $request){
        $data = $model->find($request->id);
        if($data){
            CarCost::query()->where('car_id',$data->car_id)->decrement('total_insurance_premium',$data->insurance_premium);
        }
        $data->delete();
    }
    // 获取保险公司列表
    public function companyList(){
        $data = InsureCompany::query()->select('name')->get();
        return $this->success($data);
    }
}
